package com.ganzi.ganziserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ganzi.ganziserver.core.SchoolInformation;
import com.ganzi.ganziserver.core.User;
import com.ganzi.ganziserver.service.SchoolInformationService;
import com.ganzi.ganziserver.service.SendService;
import com.ganzi.ganziserver.service.SystemLogService;
import com.ganzi.ganziserver.shiro.redis.OtherCache.UserCache;
import com.ganzi.ganziserver.utils.ReturnMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sendMessageToSchool")
public class SendController {
    private static Logger log = LoggerFactory.getLogger(SendController.class);

    @Autowired
    SendService sendService;

    @Autowired
    SystemLogService systemLogService;

    @Autowired
    SchoolInformationService schoolgService;

    /**
     * @Param
     * 单个盘管控制
     *  fanId:对应盘管的id
     * `type:控制指令类型
     *  value:指令的内容
     * */
    @RequiresPermissions("control")
    @RequestMapping(value = "/sendOneControl" ,method = {RequestMethod.POST})
    public Object sendOne(@RequestBody String json) {
        if(StringUtils.isNotBlank(json)){
            try{
                JSONObject jsons = (JSONObject) JSON.parse(json);
                JSONObject info = jsons.getJSONObject("json");
                long fanId = info.getLong("fanId");
                String type = info.getString("type");
                String value = info.getString("value");
                if(StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value) && fanId != 0l){
                    try{
                        int code = sendService.sendOneControlMessage(fanId, type, value);
                        if(code == 0){
                            return ReturnMap.getReturnMap(200,"发送成功");
                        }else{
                            return ReturnMap.getReturnMap(400,"发送失败,消息通道异常");
                        }
                    }catch (Exception e){
                        String msg = "send control message fail!:"+e.getMessage();
                        log.info(msg);
                        systemLogService.errorLog(msg);
                        return  ReturnMap.getReturnMap(400,"发送失败,系统异常");
                    }
                }else{
                    return ReturnMap.getReturnMap(400,"发送失败,指令类型和值不能为空！");
                }
            }catch (Exception e){
                log.info("error:"+e.getMessage());
                return ReturnMap.getReturnMap(400,"系统错误！");
            }
        }else{
            return ReturnMap.getReturnMap(400,"发送失败,指令类型和值不能为空！");
        }
    }




    /**
     * @Param
     * 学校盘管控制
     *  fanId:对应盘管的id
     * `type:控制指令类型
     *  value:指令的内容
     * */
    @RequiresPermissions("control")
    @RequestMapping(value = "/sendManyControl" ,method = {RequestMethod.POST})
    public Object sendMany(@RequestBody String json) {
        if(StringUtils.isNotBlank(json)){
            try{
                JSONObject jsons = (JSONObject) JSON.parse(json);
                JSONObject info = jsons.getJSONObject("json");
                long schoolId = info.getLongValue("schoolId");
                String type = info.getString("type");
                String value = info.getString("value");
                if(StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value) && schoolId != 0l){
                    try{
                        int code = sendService.sendManyControlMessage(schoolId, type, value);
                        if(code == 0){
                            return ReturnMap.getReturnMap(200,"发送成功");
                        }else{
                            return ReturnMap.getReturnMap(400,"发送失败,消息通道异常");
                        }
                    }catch (Exception e){
                        String msg = "send control message fail!:"+e.getMessage();
                        log.info(msg);
                        systemLogService.errorLog(msg);
                        return  ReturnMap.getReturnMap(400,"发送失败,系统异常");
                    }
                }else{
                    return ReturnMap.getReturnMap(400,"发送失败,指令类型和值不能为空！");
                }
            }catch (Exception e){
                log.info("error:"+e.getMessage());
                return ReturnMap.getReturnMap(400,"系统错误！");
            }
        }else{
            return ReturnMap.getReturnMap(400,"发送失败,指令类型和值不能为空！");
        }
    }



    /**
     * @Param
     *  查询学校所有盘管的状态信息
     * */
    @RequiresPermissions("queryfan")
    @RequestMapping(value = "/queryAll" ,method = {RequestMethod.POST})
    public Object queryAll(@RequestBody String json) {
        if(StringUtils.isNotBlank(json)){
            try{
                JSONObject jsons = (JSONObject) JSON.parse(json);
                long schoolId = jsons.getLongValue("schoolId");
                if(schoolId != 0l){
                    try{
                        int code = sendService.sendcollectionMessage(schoolId);
                        if(code == 0){
                            return ReturnMap.getReturnMap(200,"发送成功");
                        }else{
                            return ReturnMap.getReturnMap(400,"发送失败,消息通道异常");
                        }
                    }catch (Exception e){
                        String msg = "send control message fail!:"+e.getMessage();
                        log.info(msg);
                        systemLogService.errorLog(msg);
                        return ReturnMap.getReturnMap(400,"发送失败,系统异常");
                    }
                }else{
                    return ReturnMap.getReturnMap(400,"发送失败,指令类型和值不能为空！");
                }
            }catch (Exception e){
                log.info("error:"+e.getMessage());
                return ReturnMap.getReturnMap(400,"系统错误！");
            }
        }else{
            return ReturnMap.getReturnMap(400,"发送失败,学校ID不能为空！");
        }
    }



    /**
     * @Param
     *  查询学校和采集器在线状态 根据用户
     * */
    @RequestMapping(value = "/querystate" ,method = {RequestMethod.POST})
    public Object querystate() {
        try{
            int logo = sendService.sendStateMessageByUser(true);
            if(logo==1){
                return ReturnMap.getReturnMap(200,"成功");
            }else{
                return ReturnMap.getReturnMap(200, "刷新成功");
            }
        }catch (Exception e){
            return ReturnMap.getReturnMap(400, "服务器异常");
        }
    }


    /**
     * @Param
     *  查询采集器
     * */
    @RequiresPermissions("querycollection")
    @RequestMapping(value = "/queryCollection" ,method = {RequestMethod.POST})
    public Object queryCollection() {
        try{
            int code = sendService.sendStateMessageForCollection();
            if(code == 0){
                return ReturnMap.getReturnMap(200,"发送成功");
            }else{
                return ReturnMap.getReturnMap(400,"发送失败,消息通道异常");
            }
        }catch (Exception e){
            String msg = "send control message fail!:"+e.getMessage();
            log.info(msg);
            systemLogService.errorLog(msg);
            return ReturnMap.getReturnMap(400,"发送失败,系统异常");
        }
    }
}
